package me.zhanshi123.lab4.service.impl;

import me.zhanshi123.lab4.dao.CategoryDao;
import me.zhanshi123.lab4.entity.ApiResponse;
import me.zhanshi123.lab4.entity.Category;
import me.zhanshi123.lab4.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService implements ICategoryService {
    @Autowired
    private CategoryDao categoryDao;

    public String listAll() {
        ApiResponse<List<Category>> apiResponse = new ApiResponse<>();
        List<Category> categories = categoryDao.getAll();
        if (categories == null) {
            return apiResponse.fail("数据库操作失败");
        }
        return apiResponse.success(categories);
    }

    public String add(String name) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        int result = categoryDao.addCategory(name);
        if (result == 0) {
            return apiResponse.fail("数据库操作失败");
        }
        return apiResponse.success("ok");
    }
}
